from common.Utils import SHPDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver import ActionChains, Keys
from selenium.common.exceptions import TimeoutException  # 确保导入超时异常类
#测试商城系统收藏页测试
class FavoritesPage:
    def __init__(self):
        self.url = "http://49.235.61.184/"
        self.driver = SHPDriver.driver
        self.driver.get(self.url)
        self.wait = WebDriverWait(self.driver, 15)  # 全局显式等待（适配详情页加载慢）

    def favoritesTest1(self):
        # 1. 登录校验（收藏页需登录访问）
        login_success = SHPDriver.Auto_Login()

        # 2. 进入收藏页（通过顶部导航栏入口，避免直接访问URL需登录跳转）
        favor_entry = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a"))
        )
        favor_entry.click()
        # 进入商品收藏列表
        product_favor_link = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > ul > li > a > p"))
        )
        product_favor_link.click()
        print("已进入商品收藏页")
        # 3. 页面元素检查（显式等待，确保元素加载完成）
        # 检查商品收藏字样
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.am-flex.am-flex-items-center.am-margin-bottom-main > h1"))
        )
        # 检查收藏商品筛选框
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.am-form-table-data-list-container.am-scrollable-horizontal.am-table-scrollable-horizontal.am-radius > table > thead > tr"))
        )
        # 检查搜索按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > button.am-btn.am-radius.btn-loading-example.form-table-operate-top-search-submit"))
        )
        # 检查刷新按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > a > i"))
        )
        # 检查设置按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > button:nth-child(7) > i"))
        )
        # 检查分页框
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul"))
        )
        # 检查详情按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            ".am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-default.am-btn-xs.am-radius.am-btn-block.submit-popup > span"))
        )
        # 检查删除按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            ".am-nowrap-initial.am-text-middle.am-text-center.am-grid-fixed-right.am-operate-grid.am-grid-fixed-right-shadow > button.am-btn.am-btn-danger.am-btn-xs.am-radius.am-btn-block.submit-delete > span"))
        )
        # 检查批量删除按钮
        self.wait.until(
            EC.presence_of_element_located((By.CSS_SELECTOR,
                                            "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > button"))
        )

        SHPDriver.GetScreenShot("favorites_check_elements")
        # 4. 功能测试
        # 分页框功能
        # 设置每页两条商品
        page_size_input = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.current-page-input > input"))
        )
        # 双击删除原有值，输入2并回车
        ActionChains(self.driver).double_click(page_size_input).send_keys(Keys.DELETE).perform()
        page_size_input.send_keys("2", Keys.ENTER)
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_set_page_size")

        # 测试页数上下页跳转
        # 下一页
        next_page_btn = self.wait.until(
            EC.element_to_be_clickable(
                (By.XPATH, "/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/div[2]/ul/li[6]/a"))
        )
        next_page_btn.click()
        # 上一页
        prev_page_btn = self.wait.until(
            EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/div[2]/ul/li[2]"))
        )
        prev_page_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_prev_next_page")

        # 测试页数收尾页跳转
        # 最后一页
        last_page_btn = self.wait.until(
            EC.element_to_be_clickable((By.XPATH,
                                        "//li[contains(@class, 'last-after-page-submit')]//a[contains(@class, 'am-icon-angle-double-right')]"))
        )
        last_page_btn.click()
        time.sleep(2)
        # 第一页
        first_page_btn = self.wait.until(
            EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'am-icon-angle-double-left')]"))
        )
        first_page_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_first_last_page")

        # 测试跳转指定页数（第3页）
        target_page_input = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > div.form-table-operate-page.am-cf > ul > span.to-page-input > input"))
        )
        ActionChains(self.driver).double_click(target_page_input).send_keys(Keys.DELETE).perform()
        target_page_input.send_keys("3", Keys.ENTER)
        time.sleep(1)

        # 删除收藏的商品
        time.sleep(2)
        # 单个删除
        delete_single_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, "button.submit-delete > span"))
        )
        delete_single_btn.click()
        # 确认删除
        confirm_delete_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, ".am-modal-footer > span:nth-child(2)"))
        )
        confirm_delete_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_delete_single")

        try:
            WebDriverWait(self.driver, 5).until(  # 单独设置5秒超时
                EC.presence_of_element_located((By.CSS_SELECTOR, "table tbody tr"))
            )
            print("存在商品，已移除批量删除步骤")
            SHPDriver.GetScreenShot("products_exist_skip_batch_delete")
        except TimeoutException:
            print("没有商品，无需批量删除")
            SHPDriver.GetScreenShot("no_products_skip_batch_delete")

        # 查找收藏的商品（搜索关键词"手机"）
        search_input = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.am-form-table-data-list-container.am-scrollable-horizontal.am-table-scrollable-horizontal.am-radius > table > thead > tr > th.form-table-search-item-head-title-goods.form-table-search-item-head-value-goods.am-text-top.am-grid-lg > div > input"))
        )
        search_input.clear()
        search_input.send_keys("手机")
        # 点击搜索按钮
        search_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > button.am-btn.am-radius.btn-loading-example.form-table-operate-top-search-submit"))
        )
        search_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_search_mobile")

        # 筛选3000-4000元的手机
        min_price_input = self.wait.until(
            EC.element_to_be_clickable((By.XPATH,
                                        "/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[3]/div/div[2]/input[1]"))
        )
        min_price_input.send_keys("3000")
        max_price_input = self.wait.until(
            EC.element_to_be_clickable((By.XPATH,
                                        "/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[3]/table/thead/tr/th[3]/div/div[2]/input[2]"))
        )
        max_price_input.send_keys("4000")
        # 再次点击搜索
        search_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_filter_price")

        # 清空搜索条件（点击刷新按钮）
        refresh_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > a"))
        )
        refresh_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_clear_search")

        # 设置筛选类型
        setting_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "body > div.body-content-container > div.body-content-formal-container > div.am-form-table-data-container.am-container.user-main > div.user-content > div > div.form-table-content > form > div.form-table-operate-top.am-cf > div > div > button:nth-child(7)"))
        )
        setting_btn.click()
        # 选择筛选项（第3、4项）
        filter_item3 = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "#form-table-fields-popup > div > div.am-popup-bd > form > div.form-table-fields-select-container > div > ul > li:nth-child(3) > label > span.am-ucheck-icons > i.am-icon-checked"))
        )
        filter_item3.click()
        filter_item4 = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "#form-table-fields-popup > div > div.am-popup-bd > form > div.form-table-fields-select-container > div > ul > li:nth-child(4) > label > span.am-ucheck-icons > i.am-icon-checked"))
        )
        filter_item4.click()
        # 确认筛选设置
        confirm_filter_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "#form-table-fields-popup > div > div.am-popup-bd > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example.form-table-field-confirm-submit"))
        )
        confirm_filter_btn.click()
        time.sleep(3)
        SHPDriver.GetScreenShot("favorites_set_filter")

        # 重置筛选类型
        reset_filter_btn = self.wait.until(
            EC.element_to_be_clickable(
                (By.XPATH, "/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[2]/div/div/button[2]/i"))
        )
        reset_filter_btn.click()
        # 确认重置
        confirm_reset_btn = self.wait.until(
            EC.element_to_be_clickable((By.CSS_SELECTOR,
                                        "#form-table-fields-popup > div > div.am-popup-bd > form > div.am-form-popup-submit > button.am-btn.am-btn-primary-plain.am-radius.am-btn-xs.btn-loading-example.am-margin-right-lg.submit-ajax"))
        )
        confirm_reset_btn.click()
        time.sleep(1)
        SHPDriver.GetScreenShot("favorites_reset_filter")

# 执行测试
if __name__ == "__main__":
    favorites_page = FavoritesPage()
    favorites_page.favoritesTest1()
    # 保留浏览器5秒观察
    time.sleep(5)
    SHPDriver.quit_driver()